// Vue
import Vue from 'vue'
import App from './App'
// 核心插件
import d2Admin from '@/plugin/d2admin'
import ecFunction from '@/plugin/function'
// store
import store from '@/store/index'
// 指令
import '@/plugin/direct/dialogDrag'
// 多国语
import i18n from './i18n'
// [ 可选组件 ] 右键菜单
import contentmenu from 'v-contextmenu'
import 'v-contextmenu/dist/index.css'

import util from '@/libs/util'
// 菜单和路由设置
import router from './router'
import VueLazyload from 'vue-lazyload'
// 导入插件
import pluginImport from '@d2-projects/vue-table-import'
Vue.use(pluginImport)

import {
    menuHeader,
    menuAside
} from '@/router/routes'

// 使用VueLazyload并传入相应的配置
Vue.use(VueLazyload, {
    error: require('./assets/image/error.jpg'),
    loading: require('./assets/image/loading.gif'), // 懒加载使用的图片
})
// 全局方法
Vue.prototype.isAuth = util.isAuth // 权限方法

// 核心插件
Vue.use(d2Admin)
Vue.use(ecFunction)
Vue.use(contentmenu)
new Vue({
    router,
    store,
    i18n,
    render: h => h(App),
    created() {},
    mounted() {
        // 展示系统信息
        this.$store.commit('d2admin/releases/versionShow')
        // 用户登录后从数据库加载一系列的设置
        this.$store.dispatch('d2admin/account/load')
        // 获取并记录用户 UA
        this.$store.commit('d2admin/ua/get')
        // 初始化全屏监听
        this.$store.dispatch('d2admin/fullscreen/listen')
    },
    watch: {
        // 检测路由变化切换侧边栏内容
        '$route.matched': {
            handler(matched) {
                if (matched.length > 0) {
                    try {
                        let curPath = matched[1].path;
                        let moduleCode = "";
                        if (curPath.substr(0, 1) === '/') {
                            moduleCode = this.getMouleCode(curPath.slice(1));
                        } else {
                            moduleCode = this.getMouleCode(curPath);
                        }
                        console.log("moduleCode",moduleCode)
                        const menus = this.$store.getters['d2admin/menu/getFullAsides'];
                        const _side = menus.filter(menu => menu.path === moduleCode)
                        console.log(_side)
                        this.$store.commit('d2admin/menu/asideSet', _side.length > 0 ? _side[0].children : [])
                    } catch (error) {
                        this.$log.push("路由错误！")
                        this.$store.commit('d2admin/menu/asideSet', [])
                    }
                }
            },
            immediate: true
        }
    },
    methods: {
        // 获取路径的模块地址
        getMouleCode(moduleCode) {
            const codes = moduleCode.split("-");
            return codes[0]
        }
    }
}).$mount('#app')